package com.zk.babala.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.zk.babala.BabalaApp;

import cn.hutool.core.util.StrUtil;

/**
 * 切换主网站
 *
 * @Author: zhangke
 * @Date: 2022/3/31 11:21
 */
public class LineUtil {
    private SharedPreferences preferences;
    private static String lineSourceTag = "lineSourceTag";
    private static String lineTag = "LineTag";
    private static String ipTag = "IpTag";
    private static String lineTagCookies = "LineCookies";

    public LineUtil() {
        preferences = BabalaApp.getContext().getSharedPreferences(lineTag, Context.MODE_PRIVATE);
    }

    public static LineUtil getLineUtil() {
        return new LineUtil();
    }


    public void setLine(String lineSource, String line) {
        preferences.edit().putString(lineSourceTag, lineSource).commit();
        preferences.edit().putString(lineTag, line).commit();
        Toast.makeText(BabalaApp.getContext(), "设置线路：" + preferences.getString(lineTag, "") + " 成功", Toast.LENGTH_LONG).show();
    }

    public void setLineCookies(String cookies) {
        preferences.edit().putString(lineTagCookies + getLineSource(), cookies).commit();
        Toast.makeText(BabalaApp.getContext(), "设置Cookies：" + cookies + " 成功", Toast.LENGTH_LONG).show();
    }

    public String getLineCookies() {
        return preferences.getString(lineTagCookies + getLineSource(), "");
    }

    public String getLine() {
        return preferences.getString(lineTag, "");
    }

    public String getLineSource() {
        return preferences.getString(lineSourceTag, "");
    }

    public void setIp(String ip) {
        preferences.edit().putString(ipTag, ip).commit();
        if (StrUtil.isEmpty(ip)) {
            RequestUtil.headers.remove("X-Forwarded-For");
            Toast.makeText(BabalaApp.getContext(), "清除ip成功", Toast.LENGTH_LONG).show();
        } else {
            RequestUtil.setHeader("X-Forwarded-For", getIp());
            Toast.makeText(BabalaApp.getContext(), "设置ip：" + preferences.getString(ipTag, "") + " 成功", Toast.LENGTH_LONG).show();
        }
    }

    public String getIp() {
        return preferences.getString(ipTag, "");
    }


    public String swapUrl(String url) {
        if (StrUtil.isEmpty(url)) {
            return getLine();
        }
        if (!url.startsWith("http")) {
            if (url.startsWith("/")) {
                url = url.substring(1);
            }
            return getLine() + url;
        }
        return url;
    }

    public String getPlayUrl(String content) {
        String playUrl = content.substring(content.indexOf("url\":\"") + 6, content.indexOf("\",\"url_next")).replace("\\/", "/");
        if (StrUtil.isBlank(playUrl)) {
            Toast.makeText(BabalaApp.getContext(), "未解析到视频播放地址 " + content, Toast.LENGTH_SHORT).show();
        }
        return decodeUnicode(playUrl);
    }

    private String decodeUnicode(String unicodeStr) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (i < unicodeStr.length()) {
            if (unicodeStr.charAt(i) == '\\' && i + 1 < unicodeStr.length() && unicodeStr.charAt(i + 1) == 'u') {
                String hexCode = unicodeStr.substring(i + 2, i + 6);
                try {
                    int codePoint = Integer.parseInt(hexCode, 16);
                    sb.append((char) codePoint);
                    i += 6;
                } catch (NumberFormatException e) {
                    sb.append(unicodeStr.charAt(i));
                    i++;
                }
            } else {
                sb.append(unicodeStr.charAt(i));
                i++;
            }
        }
        return sb.toString();
    }
}
